//
//  WFSavingsMethodDetailToolView.m
//  WeiFang
//
//  Created by WeiFang Team on 2022.
//  Copyright © 2022 WeiFang Team. All rights reserved.
//

#import "WFSavingsMethodDetailToolView.h"
#import "XLCircleProgress.h"

@implementation WFSavingsMethodDetailToolView

+ (id)instanceView {
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    return [nibView objectAtIndex:0];
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

-(void)sliderMethod:(UISlider*)slider
{
    
}
    
- (void)setData:(id)data delegate:(id)delegate {
    
    NSInteger n = 365;
    NSInteger an = kUserManager.userManager.startValue.integerValue + (n - 1) * kUserManager.userManager.dailyIncrease.integerValue;
    NSInteger sn = n * (kUserManager.userManager.startValue.integerValue + an) / 2;
    _totalMoneyLabel.text = [NSString stringWithFormat:@"￥%ld.00",sn];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *stratDate = [formatter dateFromString:kUserManager.userManager.startDate];
    formatter.dateFormat = @"yyyy.MM.dd";
    NSString *stratDateStr = [formatter stringFromDate:stratDate];
    _startDateLabel.text = [NSString stringWithFormat:@"开始时间：%@",stratDateStr];
    
    NSString *currentDateStr = [formatter stringFromDate:[NSDate date]];
    
    NSInteger day = [self getTimeIntervalWithDateString1:stratDateStr dateString2:currentDateStr];
    
    if (day >= 0) {
        if (day > 363) {
            day = 364;
        }
        _currentDayLabel.text = [NSString stringWithFormat:@"%ld",day + 1];
        
    } else {
        _currentDayLabel.text = @"0";
    }
    
    _endDateLabel.text = [NSString stringWithFormat:@"结束时间：%@",[self getTimeWithDateString:kUserManager.userManager.startDate days:365]];
    
    NSInteger n1 = day + 1;
    NSInteger an1 = kUserManager.userManager.startValue.integerValue + (n1 - 1) * kUserManager.userManager.dailyIncrease.integerValue;
    NSInteger sn1 = n1 * (kUserManager.userManager.startValue.integerValue + an1) / 2;
    
    if (day >= 0) {
        _currentMoneyLabel.text = [NSString stringWithFormat:@"￥%ld.00",sn1];
    } else {
        _currentMoneyLabel.text = @"￥0.00";
    }
    
    
    // 移除之前的子控件
    [_topView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    CGFloat circleWidth = _topView.frame.size.width;
    XLCircleProgress *circle = [[XLCircleProgress alloc] initWithFrame:CGRectMake(0, 0, circleWidth, circleWidth)];
    if (day >= 0) {
        circle.progress = 1.0 * sn1 / sn;
    } else {
        circle.progress = 0;
    }
    [_topView addSubview:circle];
    
    // 移除之前的子控件
    [_bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    XLCircleProgress *circle1 = [[XLCircleProgress alloc] initWithFrame:CGRectMake(0, 0, circleWidth, circleWidth)];
    if (day >= 0) {
        circle1.progress = 1.0 * (day + 1) / 365;
    } else {
        circle1.progress = 0.0;
    }
    [_bottomView addSubview:circle1];
}

- (NSInteger)getTimeIntervalWithDateString1:(NSString *)dateString1 dateString2:(NSString *)dateString2 {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *date1 = [dateFormatter dateFromString:dateString1];
    NSDate *date2 = [dateFormatter dateFromString:dateString2];

    NSTimeInterval timeInterval = [date2 timeIntervalSinceDate:date1];
    NSInteger days = (NSInteger)timeInterval/(24*60*60);

    NSLog(@"%@与%@之间相隔%ld天", dateString1, dateString2, days);
    return days;
}

- (NSString *)getTimeWithDateString:(NSString *)dateString days:(NSInteger)days {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *date = [dateFormatter dateFromString:dateString];

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:days];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *resultDate = [calendar dateByAddingComponents:comps toDate:date options:0];
    [dateFormatter setDateFormat:@"yyyy.MM.dd"];
    NSString *resultString = [dateFormatter stringFromDate:resultDate];
    NSLog(@"%@的%ld天之后的日期为%@", dateString, days, resultString);
    return resultString;
}

@end
